import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '../stores/authStore';
import Login from '../views/Login.vue'; // 我们已经创建了这个页面
import Layout from '../components/Layout.vue'; // 我们也创建了主布局
import AdminLayout from '@/components/AdminLayout.vue';
import Placeholder from '../views/teacher/Placeholder.vue';
import { useMascotStore } from '../stores/mascotStore';
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/',
    // 访问根路径时，默认重定向到登录页
    redirect: '/login'
  },
  
  // --- 后续步骤将在这里添加管理员登录后的页面 ---
  {
    path: '/admin',
    name: 'AdminLayout',
    component: AdminLayout,
    meta: { requiresAuth: true },
    redirect: '/admin/teachers',
    children: [
      {
        path: 'teachers',
        name: 'AdminDashboard',
        component: () => import('../views/admin/adminDashboard.vue'),
      },
      {
        path: 'classroom-approval',
        name: 'ClassroomApproval',
        component: () => import('../views/admin/Classroom.vue'),
      },
      // 你可以继续添加更多子页面
    ]
  },


  // --- 后续步骤将在这里添加教师登录后的页面 ---
  // 下一步，当我们创建第一个教师页面（如Dashboard）时，
  // 我们将在这里添加如下结构：
  
  {
    path: '/teacher',
    name: 'TeacherLayout',
    component: Layout,
    meta: { requiresAuth: true }, // 所有子路由都需要登录
    redirect: '/teacher/dashboard', // 访问/teacher时，默认重定向到工作台
    children: [
      {
        path: 'dashboard',
        name: 'TeacherDashboard',
        component: () => import('../views/teacher/Dashboard.vue'), 
      },
      {
        path: 'courses',
        name: 'CourseManagement',
        component: () => import('../views/teacher/CourseManagement.vue'),
      },
      // --- (核心修改) 新增教学评价和问题中心的路由 ---
      
      {
        path: 'resources',
        name: 'resourceManagement',
        component: () => import('../views/teacher/ResourceManagement.vue'),
      },
      // {
      //   path: 'exams',
      //   name: 'ExamManagement',
      //   component: () => import('../views/teacher/ExamManagement.vue'),
      // },
      {
        path: 'homework',
        name: 'HomeworkManagement',
        component: () => import('../views/teacher/HomeworkManagement.vue'),
      },
      {
        path: 'evaluation',
        name: 'TeacherEvaluation',
        component: () => import('../views/teacher/Evaluation.vue'),
      },

      {
        path: 'recordings',
        name: 'TeacherRecordings',
        component: () => import('../views/teacher/Recordings.vue'),
      },
      {
        path: 'ai-assistant',
        name: 'TeacherAiAssistant',
        component: () => import('../views/teacher/AiAssistant.vue'),
      },
      {
        path: 'auto-grader',
        name: 'TeacherAutoGrader',
        component: () => import('../views/teacher/AutoGrader.vue'),
      },
      {
        path: 'q-a-center',
        name: 'TeacherQuestionCenter',
        component: () => import('../views/teacher/QuestionCenter.vue'),
      },
      {
        path: 'live-room',
        name: 'TeacherLiveRoom',
        component: () => import('../views/teacher/LiveRoom.vue'),
      },
      {
        path: 'create-exam',
        name: 'TeacherCreateExam',
        component: () => import('../views/teacher/CreateExam.vue'),
      },
      // --- 【核心修改】替换占位符路由，添加新页面的路由 ---
      {
        path: 'exams', // 路径 /teacher/exams
        name: 'TeacherExamList',
        component: () => import('../views/teacher/ExamList.vue'),
      },
      {
        path: 'exams/preview/:id', // 路径 /teacher/exams/preview/123
        name: 'TeacherExamPreview',
        component: () => import('../views/teacher/ExamPreview.vue'),
        props: true, // 将路由参数:id作为props传递给组件
      },

      {
        path: 'profile',
        name: 'TeacherProfile',
        component: () => import('../views/teacher/Profile.vue'),
      },
      {
        path: 'clock-in',
        name: 'TeacherClockIn',
        component: () => import('../views/teacher/ClockIn.vue'),
      },
      {
        path: 'classroom-booking', // 新增的路由路径
        name: 'ClassroomBooking', // 新增的路由名称
        component: () => import('../views/teacher/ClassroomBooking.vue'), // 对应的组件
        meta: { title: '教室借阅' } // 页面标题
      },
      {
        path: 'music',
        name: 'TeacherMusicCenter',
        component: () => import('../views/teacher/MusicCenter.vue')
      },
      {
        path: 'forum',
        name: 'TeacherForum',
        component: () => import('../views/teacher/Forum.vue')
      },
      {
        path: 'document-solver',
        name: 'TeacherDocumentSolver',
        component: () => import('../views/teacher/DocumentSolver.vue'),
        meta: { title: '文档题目解析' }
      },
      {
        path: 'navigation',
        name: 'TeacherNavigation',
        component: () => import('../views/teacher/Navigation.vue'),
        meta: { title: '地图导航' }
      },
      {
        path: 'resource-center',
        name: 'TeacherResources',
        component: () => import('../views/teacher/ResourceCenter.vue')
      },
      {
        path: 'plagiarism-check',
        name: 'TeacherPlagiarismCheck',
        component: () => import('../views/teacher/PlagiarismCheck.vue'),
        meta: { title: '学术诚信分析' }
      },
      {
        path: 'quiz-game',
        name: 'TeacherQuizGame',
        component: () => import('../views/teacher/QuizGame.vue'),
        meta: { title: '答题闯关' }
      },
      {
        path: 'personal-information', // 新路径，例如：/teacher/personal-information
        name: 'TeacherPersonalInfo', // 新路由名
        component: () => import('../views/teacher/PersonalInformation.vue'),
        meta: { title: '个人信息' }
      },
      {
        path:'simulation-lab',
        name: 'TeacherSimulationLab',
        component: () => import('../views/teacher/SimulationLab.vue'),
        meta: { title: '模拟实验中心' }
      },
      {
        path: 'classroom-3d',
        name: 'TeacherClassroom3D',
        component: () => import('../views/teacher/Classroom3D.vue'),
        meta: { title: '3D教室预览' }
      },
      
      // {
      //   path: '/course-management',
      //   name: 'CourseManagement',
      //   component: () => import('../views/teacher/CourseManagement.vue')
      // },

      { path: 'training', name: 'TeacherTraining', component: () => import('../views/teacher/Training.vue') },
      { path: 'assessments', name: 'TeacherAssessments', component: () => import('../views/teacher/Assessments.vue') },
      { path: 'grades', name: 'TeacherGrades', component: () => import('../views/teacher/Grades.vue') },
      { path: 'surveys', name: 'TeacherSurveys', component: ()=> import('../views/teacher/Surveys.vue') },

    
    ]
  },
  
];


const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    next({ name: 'Login' });
  } else if (to.name === 'Login' && authStore.isAuthenticated) {
    next({ name: 'TeacherDashboard' });
  } else {
    next();
  }
});

router.afterEach((to) => {
  const mascot = useMascotStore();
  mascot.gainEnergyOnActivity();
  mascot.triggerContextualTip(to.name);
});

export default router;